package com.ruoyi.app.controller;

import com.ruoyi.app.domain.MemberUser;
import com.ruoyi.app.domain.OperStaff;
import com.ruoyi.app.service.IMemberUserService;
import com.ruoyi.common.constant.ConstantsRedis;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.jwt.JwtHelper;
import com.ruoyi.common.vo.MemberUserVo;
import com.ruoyi.common.vo.OnlineUserList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/app/user/")
public class ApiUserController extends BaseController {

    @Autowired
    private IMemberUserService memberUserService;

    @GetMapping("/get")
    @ResponseBody
    public AjaxResult getMemberInfo(HttpServletRequest request) {

        String id = getMemberId(request);

        OperStaff operStaff = memberUserService.selectMemberBaseById(id);

        operStaff.setPassword("");
//        String nickname = member.getNickname().substring(0, 3)+"****" + member.getNickname().substring(7);
//        member.setNickname(nickname);

        return AjaxResult.success("成功",operStaff);
    }
    @PostMapping("/logout")
    @ResponseBody
    public AjaxResult logout(HttpServletRequest request) {

        String id = getMemberId(request);

        OperStaff operStaff = memberUserService.selectMemberBaseById(id);

        JwtHelper.generateToken(operStaff.getLoginName(),String.valueOf(operStaff.getId()));

        // 从在线用户列表中移除用户的登录状态
        OnlineUserList.removeUser(operStaff.getLoginName());

        return AjaxResult.success("退出成功");
    }
}
